package com.my.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.common.constans.UserAuditiConstans;
import com.my.model.admin.dtos.UserAuditDto;
import com.my.model.common.dtos.PageRequestDto;
import com.my.model.common.dtos.ResponseResult;
import com.my.model.common.enums.AppHttpCodeEnum;
import com.my.model.user.pojos.ApUser;
import com.my.model.user.pojos.ApUserRealname;
import com.my.user.mapper.ApUserRealnameMapper;
import com.my.user.service.ApUserRealnameService;
import com.my.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    /**
     * 获取用户列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getUserList(UserAuditDto dto) {
        if(dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        log.info("获取待审核用户列表...");
        //分页查询
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ApUserRealname::getCreatedTime);
        lqw.eq(dto.getStatus() != null,ApUserRealname::getStatus,dto.getStatus());
        IPage<ApUserRealname> iPage = page(page, lqw);

        //结果封装
        List<ApUserRealname> records = iPage.getRecords();

        return ResponseResult.okResult(records);
    }

    @Autowired
    private ApUserService apUserService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    /**
     * 审核成功
     * @param map
     * @return
     */
    @Override
    public ResponseResult auditPass(Map map) {
        log.info("开始对用户进行审核...");
        //获取id
        Integer id = (Integer) map.get("id");
        if(id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询审核对象信息
        ApUserRealname apUserRealname = getById(id);
        if(apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"待审核用户信息不存在");
        }
        //获取用户id
        Integer userId = apUserRealname.getUserId();
        //将用户flag设置成自媒体人（1）
        ApUser apUser = apUserService.getById(userId);
        if(apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"App端用户信息不存在");
        }
        log.info("将App端该用户更新为自媒体人...");
        apUser.setFlag((short)1);
        apUserService.updateById(apUser);

        //调用Kafka发送消息
        kafkaTemplate.send(UserAuditiConstans.USER_AUDIT_TOPIC, JSON.toJSONString(apUser));
        log.info("调用Kafka发送消息给自媒体微服务...");

        //更新用户状态
        apUserRealname.setStatus(ApUserRealname.Status.SUCCESS.getCode());
        updateById(apUserRealname);
        log.info("更新用户审核状态....");

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核失败
     * @param map
     * @return
     */
    @Override
    public ResponseResult auditFail(Map map) {
        log.info("审核不通过...");
        //获取id
        Integer id = (Integer) map.get("id");
        String reason = (String) map.get("msg");
        if(id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询审核对象信息
        ApUserRealname apUserRealname = getById(id);
        if(apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"待审核用户信息不存在");
        }

        //更新状态
        apUserRealname.setStatus((short)2);
        apUserRealname.setReason(reason);
        updateById(apUserRealname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
